package com.vip.jwt.controller;

import com.vip.jwt.common.utils.JwtUtil;
import com.vip.jwt.config.JwtFilter;
import com.vip.jwt.entity.User;
import com.vip.jwt.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author vip
 * @date 2019/2/12 22:04
 */
@Component
public class BaseController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private HttpServletRequest request;

    @Resource
    private UserService userService;

    /**
     * 数据导出
     *
     * @param modelMap
     * @param request
     * @param response
     * @param list
     * @param title
     * @param params
     */
//    protected void report(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response,
//                          Object list, Map<String,Object> map, String title, TemplateExportParams params) {
//        if (map == null) {
//            map = new HashMap<>(16);
//        }
//        map.put("title", title);
//        map.put("date", LocalDateUtil.localDateTimeToString(LocalDateTime.now(), DateUtil.DATETIMEFORMAT));
//        map.put("report", list);
//
//        modelMap.put(TemplateExcelConstants.FILE_NAME, title);
//        modelMap.put(TemplateExcelConstants.PARAMS, params);
//        modelMap.put(TemplateExcelConstants.MAP_DATA, map);
//        PoiBaseView.render(modelMap, request, response, TemplateExcelConstants.EASYPOI_TEMPLATE_EXCEL_VIEW);
//    }

    /**
     * 获取Token
     * @return
     */
    protected String getToken(){
        return request.getHeader(JwtFilter.getAUTHORIZATION());
    }

    /**
     * 获取用户ID
     *
     * @return
     */
    protected String getUserId(){
        User user = getUser();
        return user.getId();
    }

    /**
     * 获取用户账户
     *
     * @return
     */
    protected String getUsername(){
        String token = request.getHeader(JwtFilter.getAUTHORIZATION());
        String username = JwtUtil.getUsername(token);
        Assert.notNull(username,"Token错误");
        return username;
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    protected User getUser(){
        String username = getUsername();
        User user = userService.findByUsername(username);
        Assert.notNull(user,"用户不存在");
        return user;
    }

    /**
     * 鉴权
     *
     */
//    protected boolean isAuth(String auth){
//        String token = request.getHeader(TOKEN);
//        String username = JwtUtil.getUsername(token);
//        Assert.notNull(username,"Token错误");
//        SystemUser systemUser = systemUserService.findByUsername(username);
//        SystemRole role = systemUser.getRole();
//        Assert.notNull(role,"角色为空");
//        List<SystemPermission> permission = role.getPermissions();
//        Assert.notNull(permission,"权限为空");
//        for(int i = 0; i<permission.size(); i++){
//            if(auth.equals(permission.get(i))){
//                return true;
//            }
//        }
//        return false;
//    }

    /**
     * 鉴权
     *
     */
//    protected boolean isAuth(String[] auth){
//        String token = request.getHeader(TOKEN);
//        String username = JwtUtil.getUsername(token);
//        Assert.notNull(username,"Token错误");
//        SystemUser systemUser = systemUserService.findByUsername(username);
//        SystemRole role = systemUser.getRole();
//        Assert.notNull(role,"角色为空");
//        List<SystemPermission> permission = role.getPermissions();
//        Assert.notNull(permission,"权限为空");
//        for(int i=0;i<auth.length;i++){
//            for(int j=0;j<permission.size();j++){
//                if(auth[i].equals(permission.get(i))){
//                    return true;
//                }
//            }
//        }
//        return false;
//    }

}
